%% pattern examples for the pdf documentation

-module(patdemo).

-include("class1.hrl").
-include("class2.hrl").

-export([listtest/0, composite/1, composite2/1]).

-record(rec, {a, b, c}).

test2(ObjList) ->
    [_,#class1{field1 = 5, field2 = B},#class2{z = {A, B}}|_] = ObjList.

test2r(ObjList) ->
    [_,#class1{field1 = 5, field2 = #class1{field3 = B}},#class2{z = {A, B}}|_] = ObjList.

clausedemo1(A = {_, #class1{field2 = B}}, C) when C > 5 ->
    A+B+C;
clausedemo1(A = {_, #class1{field2 = B, field3 = xyz}}, C) when C < 5 ->
    A+B-C;
clausedemo1(A = {_, #class1{field2 = B, field3 = C}}, C) ->
    A+B-C;
clausedemo1(A = {_, #class1{field2 = B}}, C) when B =:= 42 ->
    A+B-C.


clausedemo2([_,#class1{field1 = A, field2 = A}], #class2{field3 = A}) ->
    A.

clausedemo3([_,#class1{field1 = A, field2 = #class2{z = #rec{b = Z}}}, #class1{field3 = [_, {_,_,Z}]}], Z) ->
    A+Z.

lt0([A, B, C|_]) when C > 2 ->
    {A, B, C}.

lt1(L) when hd(tl(tl(L))) > 2 ->
    {hd(L), hd(tl(L)), hd(tl(tl(L)))};
lt1(_) ->
    nomatch.

listtest() ->
    lt1(42).

xyz(A, Z) when {a, Z} =:= element(2, A) ->
    z.
    
composite(#class1{field1 = {bre, ke, ke}}) ->
    hey.

composite2(#class1{field1 = A}) when A =:= {bre, ke, ke} ->
    hey.